package qlsl.androiddesign.listener;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import android.widget.Toast;
import qlsl.androiddesign.activity.baseactivity.BaseActivity;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.constant.MessageConstant;
import qlsl.androiddesign.manager.ActivityManager;
import qlsl.androiddesign.util.commonutil.Log;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 上传图片,音频等文件的公用回调<br/>
 * 
 */
public abstract class OnUploadFileListener extends RequestCallBack<String> {

	public void onSuccess(ResponseInfo<String> responseInfo) {
		hideProgressBar();
		String responseStr = responseInfo.result;
		Log.e("qlsl", "responseStr :" + responseStr);
		JSONObject jo = JSONObject.parseObject(responseStr);
		if (jo == null) {
			ActivityManager.getInstance().showToast(
					MessageConstant.MSG_SERVER_FAILED);
			return;
		}
		if ("success".equals(jo.getString("msg"))) {
			ActivityManager.getInstance().showToast("上传成功");
			onSuccess(jo);
		} else if ("fail".equals(jo.getString("msg"))) {
			String errorCode = jo.getString("errorCode");
			if ("40001".equals(errorCode)) {
				ActivityManager.getInstance().showToast("上传失败");
			} else {
				ActivityManager.getInstance().showToast("服务器错误");
			}
		} else if ("40002".equals(jo.getString("msg"))) {
			ActivityManager.getInstance().showToast("补全店铺资料，才能发布商品");
		} else {
			ActivityManager.getInstance().showToast("" + jo.getString("msg"));
//			 Log.e("zcj", responseInfo.result);
//			 ActivityManager.getInstance().showToast("参数错误");
		}
	}

	public void onLoading(long total, long current, boolean isUploading) {
		if (isUploading) {
			long progress = current * 100 / total;
			String text = "正在上传：" + progress + "%";
			FunctionView<?> functionView = ((BaseActivity) ActivityManager
					.getInstance().currentActivity()).getFunctionView();
			functionView.setProgressBarText(text);
			functionView.showProgressBar();
		}
	}

	public void onFailure(HttpException error, String msg) {
		hideProgressBar();
		Toast.makeText(SoftwareApplication.getInstance(), "上传失败：" + msg,
				Toast.LENGTH_SHORT).show();
	}

	private void hideProgressBar() {
		((BaseActivity) ActivityManager.getInstance().currentActivity())
				.getFunctionView().hideProgressBar();
	}

	public abstract void onSuccess(JSONObject jo);

}
